<template>
  <div class="home-view">
    <px-container>
      <!-- 欢迎区域 -->
      <section class="hero-section">
        <div class="hero-content">
          <h1 class="hero-title">
            <span class="pixel-text">像素风</span>
            <span class="pixel-text" style="color: var(--pixel-primary);">教室查询</span>
          </h1>
          <p class="hero-subtitle">
            使用AI助手快速查询北京交通大学教室空闲状态
          </p>

          <px-space style="margin-top: 32px;">
            <px-button
              size="large"
              theme="primary"
              @click="navigateToChat"
            >
              开始对话查询
            </px-button>
            <px-button
              size="large"
              variant="outline"
              @click="navigateToLogin"
            >
              系统登录
            </px-button>
          </px-space>
        </div>

        <div class="hero-pixel-art">
          <div class="pixel-character">
            <div class="character-head">👤</div>
            <div class="character-body">🎓</div>
          </div>
        </div>
      </section>

      <!-- 功能介绍 -->
      <section class="features-section">
        <h2 class="section-title">功能特色</h2>
        <px-grid :column="3" style="margin-top: 24px;">
          <px-grid-item :span="1" v-for="feature in features" :key="feature.id">
            <div class="feature-card pixel-border">
              <div class="feature-icon">{{ feature.icon }}</div>
              <h3 class="feature-title">{{ feature.title }}</h3>
              <p class="feature-description">{{ feature.description }}</p>
            </div>
          </px-grid-item>
        </px-grid>
      </section>

      <!-- 使用步骤 -->
      <section class="steps-section">
        <h2 class="section-title">使用步骤</h2>
        <px-space direction="vertical" style="margin-top: 24px;">
          <div
            v-for="(step, index) in steps"
            :key="step.id"
            class="step-item"
          >
            <div class="step-number pixel-border">{{ index + 1 }}</div>
            <div class="step-content">
              <h3 class="step-title">{{ step.title }}</h3>
              <p class="step-description">{{ step.description }}</p>
            </div>
          </div>
        </px-space>
      </section>

      <!-- 系统状态 -->
      <section class="status-section">
        <h2 class="section-title">系统状态</h2>
        <div class="status-container">
          <px-grid :column="2">
            <px-grid-item :span="1">
              <div class="status-card">
                <div class="status-header">
                  <span class="status-title">后端API</span>
                  <px-tag
                    :theme="systemStatus.backend ? 'success' : 'danger'"
                    size="small"
                  >
                    {{ systemStatus.backend ? '在线' : '离线' }}
                  </px-tag>
                </div>
                <div class="status-details">
                  <p>端口: 8001</p>
                  <p>状态: {{ systemStatus.backend ? '正常运行' : '连接失败' }}</p>
                </div>
              </div>
            </px-grid-item>
            <px-grid-item :span="1">
              <div class="status-card">
                <div class="status-header">
                  <span class="status-title">AI助手</span>
                  <px-tag
                    :theme="systemStatus.ai ? 'success' : 'warning'"
                    size="small"
                  >
                    {{ systemStatus.ai ? '就绪' : '初始化中' }}
                  </px-tag>
                </div>
                <div class="status-details">
                  <p>模型: GLM-4-plus</p>
                  <p>会话: {{ systemStatus.session ? '已登录' : '未登录' }}</p>
                </div>
              </div>
            </px-grid-item>
          </px-grid>
        </div>
      </section>
    </px-container>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useSystemStore } from '@/stores/system'

const router = useRouter()
const systemStore = useSystemStore()

const systemStatus = ref({
  backend: false,
  ai: false,
  session: false
})

const features = ref([
  {
    id: 1,
    icon: '🤖',
    title: 'AI智能对话',
    description: '基于自然语言的智能教室查询助手'
  },
  {
    id: 2,
    icon: '🏫',
    title: '教学楼全覆盖',
    description: '支持北交大所有教学楼的教室查询'
  },
  {
    id: 3,
    icon: '⚡',
    title: '实时查询',
    description: '获取最新的教室空闲状态信息'
  },
  {
    id: 4,
    icon: '🎨',
    title: '像素风界面',
    description: '复古游戏风格的用户界面设计'
  },
  {
    id: 5,
    icon: '📱',
    title: '响应式设计',
    description: '支持桌面和移动设备访问'
  },
  {
    id: 6,
    icon: '🔒',
    title: '安全登录',
    description: '支持教务系统安全登录验证'
  }
])

const steps = ref([
  {
    id: 1,
    title: '系统登录',
    description: '使用学号密码登录教务系统以获取查询权限'
  },
  {
    id: 2,
    title: '开始对话',
    description: '在AI对话界面输入自然语言查询请求'
  },
  {
    id: 3,
    title: '获取结果',
    description: '系统自动解析查询并返回教室空闲状态'
  }
])

const navigateToChat = () => {
  router.push('/chat')
}

const navigateToLogin = () => {
  router.push('/login')
}

const checkSystemStatus = async () => {
  try {
    const status = await systemStore.fetchSystemStatus()
    systemStatus.value = {
      backend: status.app_initialized,
      ai: status.agent_ready,
      session: status.session_active
    }
  } catch (error) {
    console.error('系统状态检查失败:', error)
    systemStatus.value = {
      backend: false,
      ai: false,
      session: false
    }
  }
}

onMounted(() => {
  checkSystemStatus()
  // 定期检查系统状态
  setInterval(checkSystemStatus, 30000) // 每30秒检查一次
})
</script>

<style scoped>
.home-view {
  padding: 20px;
  min-height: 100vh;
}

.hero-section {
  text-align: center;
  padding: 60px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 0;
  margin-bottom: 40px;
  position: relative;
  overflow: hidden;
}

.hero-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 10px,
      rgba(255, 255, 255, 0.03) 10px,
      rgba(255, 255, 255, 0.03) 20px
    );
  pointer-events: none;
}

.hero-content {
  position: relative;
  z-index: 1;
}

.hero-title {
  font-size: 48px;
  margin-bottom: 20px;
  line-height: 1.2;
  text-shadow:
    4px 4px 0 rgba(0, 0, 0, 0.8),
    8px 8px 0 rgba(0, 0, 0, 0.4);
}

.hero-subtitle {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.9);
  max-width: 600px;
  margin: 0 auto;
}

.hero-pixel-art {
  margin-top: 40px;
}

.pixel-character {
  display: inline-block;
  animation: pixelFloat 3s ease-in-out infinite;
}

.character-head {
  font-size: 60px;
  margin-bottom: -10px;
}

.character-body {
  font-size: 40px;
}

.section-title {
  font-size: 24px;
  text-align: center;
  margin-bottom: 20px;
  color: var(--pixel-primary);
  text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.3);
}

.features-section {
  margin-bottom: 60px;
}

.feature-card {
  background: white;
  padding: 24px;
  text-align: center;
  min-height: 200px;
  transition: transform 0.2s ease;
}

.feature-card:hover {
  transform: translate(-2px, -2px);
}

.feature-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.feature-title {
  font-size: 14px;
  margin-bottom: 12px;
  color: var(--pixel-primary);
}

.feature-description {
  font-size: 10px;
  color: var(--pixel-secondary);
  line-height: 1.6;
}

.steps-section {
  margin-bottom: 60px;
}

.step-item {
  display: flex;
  align-items: flex-start;
  gap: 20px;
  padding: 20px;
  background: white;
  border: 3px solid var(--pixel-primary);
  box-shadow: 4px 4px 0 rgba(0, 0, 0, 0.8);
}

.step-number {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
  color: white;
  background-color: var(--pixel-primary);
  flex-shrink: 0;
}

.step-title {
  font-size: 14px;
  margin-bottom: 8px;
  color: var(--pixel-primary);
}

.step-description {
  font-size: 10px;
  color: var(--pixel-secondary);
  line-height: 1.6;
}

.status-section {
  margin-bottom: 40px;
}

.status-container {
  background: white;
  padding: 24px;
  border: 3px solid var(--pixel-info);
  box-shadow: 4px 4px 0 rgba(0, 0, 0, 0.8);
}

.status-card {
  padding: 16px;
  border: 2px solid var(--pixel-info);
  background: rgba(59, 130, 246, 0.05);
}

.status-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.status-title {
  font-size: 12px;
  font-weight: bold;
  color: var(--pixel-info);
}

.status-details {
  font-size: 10px;
  color: var(--pixel-secondary);
}

.status-details p {
  margin-bottom: 4px;
}

@media (max-width: 768px) {
  .hero-title {
    font-size: 32px;
  }

  .hero-subtitle {
    font-size: 14px;
  }

  .section-title {
    font-size: 20px;
  }

  .step-item {
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }
}

@keyframes pixelFloat {
  0%, 100% { transform: translateY(0px); }
  50% { transform: translateY(-10px); }
}
</style>